package com.mezz.bones.im.logic.service.impl;

import com.mezz.bones.im.logic.domain.entity.ChatUserRemark;
import com.mezz.bones.im.logic.domain.request.ApiUserRemarkClearRequest;
import com.mezz.bones.im.logic.domain.request.ApiUserRemarkSettingRequest;
import com.mezz.bones.im.logic.repository.IChatUserRemarkRepository;
import com.mezz.bones.im.logic.service.IChatUserRemarkService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: zhuang
 * @Date: 2025/5/21 15:34
 * @Description: TODO
 * @Version: 1.0
 **/

@Service
@AllArgsConstructor
@Slf4j
public class ChatUserRemarkServiceImpl implements IChatUserRemarkService {

    private final IChatUserRemarkRepository chatUserRemarkRepository;

    @Override
    public void saveUserRemark(ApiUserRemarkSettingRequest request) {

        ChatUserRemark exists = chatUserRemarkRepository.getByUserIdAndPeerId(request.getUserId(), request.getPeerId());
        Date now = new Date();
        if (exists == null) {
            //新建
            ChatUserRemark add = new ChatUserRemark();
            add.setCreateTime(now);
            add.setUpdateTime(now);
            add.setUserId(request.getUserId());
            add.setPeerId(request.getPeerId());
            add.setRemarkName(request.getRemarkName());
            add.setIsDel(0);
            chatUserRemarkRepository.save(add);
            return;
        }

        //更新
        ChatUserRemark update = new ChatUserRemark();
        update.setId(exists.getId());
        update.setRemarkName(request.getRemarkName());
        update.setUpdateTime(now);
        chatUserRemarkRepository.updateById(update);
    }

    @Override
    public void clearUserRemark(ApiUserRemarkClearRequest request) {
        chatUserRemarkRepository.clearRemarkName(request.getUserId(), request.getPeerId());
    }
}
